<?xml version="1.0" encoding="UTF-8"?>
<as:AppSchemaDataAccess xmlns:as="http://www.geotools.org/app-schema"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:schemaLocation="http://www.geotools.org/app-schema AppSchemaDataAccess.xsd">
  <namespaces>
    <Namespace>
      <prefix>st</prefix>
      <uri>http://www.stations.org/1.0</uri>
    </Namespace>
    <Namespace>
      <prefix>gml</prefix>
      <uri>http://www.opengis.net/gml</uri>
    </Namespace>
  </namespaces>

  <sourceDataStores>
    <DataStore>
      <id>data_source</id>
      <parameters>
        <Parameter>
          <name>data_store</name>
          <value>mongodb://{mongoHost}:{mongoPort}/{dataBaseName}</value>
        </Parameter>
        <Parameter>
          <name>namespace</name>
          <value>http://www.stations.org/1.0</value>
        </Parameter>
        <Parameter>
          <name>schema_store</name>
          <value>file:{schemaStore}</value>
        </Parameter>
        <Parameter>
          <name>data_store_type</name>
          <value>complex</value>
        </Parameter>
        <Parameter>
          <name>objs_id_schema</name>
          <value>58e5889ce4b02461ad5af080,58e5889ce4b02461ad5af081</value>
        </Parameter>
      </parameters>
    </DataStore>
  </sourceDataStores>

  <targetTypes>
    <FeatureType>
      <schemaUri>stations.xsd</schemaUri>
    </FeatureType>
  </targetTypes>

  <typeMappings>
    <FeatureTypeMapping>
      <sourceDataStore>data_source</sourceDataStore>
      <sourceType>{collectionName}</sourceType>
      <targetElement>st:StationFeature</targetElement>
      <attributeMappings>
        <AttributeMapping>
          <targetAttribute>st:StationFeature</targetAttribute>
          <idExpression>
            <OCQL>jsonSelect('id')</OCQL>
          </idExpression>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>st:name</targetAttribute>
          <sourceExpression>
            <OCQL>jsonSelect('name')</OCQL>
          </sourceExpression>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>st:contact/st:mail</targetAttribute>
          <sourceExpression>
            <OCQL>jsonSelect('contact.mail')</OCQL>
          </sourceExpression>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>st:measurement</targetAttribute>
          <sourceExpression>
            <OCQL>collectionLink('measurements')</OCQL>
            <linkElement>st:Measurement</linkElement>
            <linkField>FEATURE_LINK[1]</linkField>
          </sourceExpression>
          <isMultiple>true</isMultiple>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>st:geometry</targetAttribute>
          <sourceExpression>
            <OCQL>jsonSelect('geometry')</OCQL>
          </sourceExpression>
        </AttributeMapping>
      </attributeMappings>
    </FeatureTypeMapping>
    <FeatureTypeMapping>
      <sourceDataStore>data_source</sourceDataStore>
      <sourceType>{collectionName}</sourceType>
      <targetElement>st:Measurement</targetElement>
      <attributeMappings>
        <AttributeMapping>
          <targetAttribute>st:Measurement</targetAttribute>
          <idExpression>
            <OCQL>collectionId('measurements')</OCQL>
          </idExpression>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>st:name</targetAttribute>
          <sourceExpression>
            <OCQL>jsonSelect('measurements.name')</OCQL>
          </sourceExpression>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>st:unit</targetAttribute>
          <sourceExpression>
            <OCQL>jsonSelect('measurements.unit')</OCQL>
          </sourceExpression>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>st:values</targetAttribute>
          <sourceExpression>
            <OCQL>collectionLink('measurements.values')</OCQL>
            <linkElement>st:Value</linkElement>
            <linkField>FEATURE_LINK[2]</linkField>
          </sourceExpression>
          <isMultiple>true</isMultiple>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>FEATURE_LINK[1]</targetAttribute>
          <sourceExpression>
            <OCQL>collectionLink('measurements')</OCQL>
          </sourceExpression>
        </AttributeMapping>
      </attributeMappings>
    </FeatureTypeMapping>
    <FeatureTypeMapping>
      <sourceDataStore>data_source</sourceDataStore>
      <sourceType>{collectionName}</sourceType>
      <targetElement>st:Value</targetElement>
      <attributeMappings>
        <AttributeMapping>
          <targetAttribute>st:Value</targetAttribute>
          <idExpression>
            <OCQL>collectionId('values')</OCQL>
          </idExpression>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>st:timestamp</targetAttribute>
          <sourceExpression>
            <OCQL>jsonSelect('values.time')</OCQL>
          </sourceExpression>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>st:value</targetAttribute>
          <sourceExpression>
            <OCQL>jsonSelect('values.value')</OCQL>
          </sourceExpression>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>FEATURE_LINK[2]</targetAttribute>
          <sourceExpression>
            <OCQL>collectionLink('values')</OCQL>
          </sourceExpression>
        </AttributeMapping>
      </attributeMappings>
    </FeatureTypeMapping>
  </typeMappings>

</as:AppSchemaDataAccess>